﻿// Copyright (c) 2008 Vesa Tuomiaro
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, 
// merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
// permit persons to whom the Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Text;
using System.Diagnostics;
using System.Globalization;
using System.Xml.Serialization;

namespace FastMath
{
    /// <summary>
    /// Representation of a box.
    /// </summary>
    [Serializable]
    [XmlType("box")]
    [DebuggerDisplay("Min = {Min} Max = {Max}")]
    public struct Box : IEquatable<Box>, IFormattable
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the structure.
        /// </summary>
        /// <param name="minX">Minimum coordinate on the X axis.</param>
        /// <param name="minY">Minimum coordinate on the Y axis.</param>
        /// <param name="minZ">Minimum coordinate on the Z axis.</param>
        /// <param name="maxX">Maximum coordinate on the X axis.</param>
        /// <param name="maxY">Maximum coordinate on the Y axis.</param>
        /// <param name="maxZ">Maximum coordinate on the Z axis.</param>
        public Box(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
        {
            this.Min.X = minX;
            this.Min.Y = minY;
            this.Min.Z = minZ;
            this.Max.X = maxX;
            this.Max.Y = maxY;
            this.Max.Z = maxZ;
        }

        /// <summary>
        /// Initializes a new instance of the structure.
        /// </summary>
        /// <param name="min">Minimum corner of the box.</param>
        /// <param name="max">Maximum corner of the box.</param>
        public Box(ref Vector3 min, ref Vector3 max)
        {
            this.Min = min;
            this.Max = max;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Indicates whether the current object and a specified object are equal.
        /// </summary>
        /// <param name="obj">Another object to compare to.</param>
        /// <returns><c>true</c> if <paramref name="obj"/> and the current object are the same type and represent the same value; otherwise, <c>false</c>.</returns>
        public override bool Equals(object obj)
        {
            if (obj is Box)
            {
                return Equals((Box)obj);
            }

            return false;
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns><c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.</returns>
        public bool Equals(Box other)
        {
            return
                this.Min.X == other.Min.X &&
                this.Min.Y == other.Min.Y &&
                this.Min.Z == other.Min.Z &&
                this.Max.X == other.Max.X &&
                this.Max.Y == other.Max.Y &&
                this.Max.Z == other.Max.Z;
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <param name="delta">Maximum allowed error.</param>
        /// <returns><c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.</returns>
        public bool Equals(Box other, float delta)
        {
            return
                Math.Abs(this.Min.X - other.Min.X) <= delta &&
                Math.Abs(this.Min.Y - other.Min.Y) <= delta &&
                Math.Abs(this.Min.Z - other.Min.Z) <= delta &&
                Math.Abs(this.Max.X - other.Max.X) <= delta &&
                Math.Abs(this.Max.Y - other.Max.Y) <= delta &&
                Math.Abs(this.Max.Z - other.Max.Z) <= delta;
        }

        /// <summary>
        /// Returns the hash code for the current object. 
        /// </summary>
        /// <returns>A 32-bit signed integer that is the hash code for the current object.</returns>
        public override int GetHashCode()
        {
            return
                this.Min.GetHashCode() +
                this.Max.GetHashCode();
        }

        /// <summary>
        /// Gets the string representation of the current object.
        /// </summary>
        /// <returns>A <see cref="String"/> containing a human-readable representation of the object.</returns>
        public override string ToString()
        {
            return this.ToString("F", CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Formats the value of the current object using the specified format.
        /// </summary>
        /// <param name="format">The <see cref="System.String"/> specifying the format to use. -or- null to use the default format defined for the type of the <see cref="System.IFormattable"/> implementation. </param>
        /// <param name="formatProvider">The <see cref="System.IFormatProvider"/> to use to format the value.-or- null to obtain the numeric format information from the current locale setting of the operating system. </param>
        /// <returns>A <see cref="String"/> containing a human-readable representation of the object.</returns>
        public string ToString(string format, IFormatProvider formatProvider)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append('{');
            sb.Append(this.Min.ToString(format, formatProvider));
            sb.Append(' ');
            sb.Append(this.Max.ToString(format, formatProvider));
            sb.Append('}');
            return sb.ToString();
        }
        #endregion

        #region Operators
        /// <summary>
        /// Determines whether two instances are equal.
        /// </summary>
        /// <param name="value1">A <see cref="Box"/>.</param>
        /// <param name="value2">A <see cref="Box"/>.</param>
        /// <returns>A boolean value indicating whether the instances are equal.</returns>
        public static bool operator ==(Box value1, Box value2)
        {
            return
                value1.Min.X == value2.Min.X &&
                value1.Min.Y == value2.Min.Y &&
                value1.Min.Z == value2.Min.Z &&
                value1.Max.X == value2.Max.X &&
                value1.Max.Y == value2.Max.Y &&
                value1.Max.Z == value2.Max.Z;
        }

        /// <summary>
        /// Determines whether two instances are not equal.
        /// </summary>
        /// <param name="value1">A <see cref="Box"/>.</param>
        /// <param name="value2">A <see cref="Box"/>.</param>
        /// <returns>A boolean value indicating whether the instances are not equal.</returns>
        public static bool operator !=(Box value1, Box value2)
        {
            return
                value1.Min.X != value2.Min.X ||
                value1.Min.Y != value2.Min.Y ||
                value1.Min.Z != value2.Min.Z ||
                value1.Max.X != value2.Max.X ||
                value1.Max.Y != value2.Max.Y ||
                value1.Max.Z != value2.Max.Z;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the empty box.
        /// </summary>
        /// <value>
        /// A <see cref="Box"/>.
        /// </value>
        /// <remarks>
        /// This property is read-only.
        /// </remarks>
        public static Box Empty
        {
            get { return empty; }
        }

        /// <summary>
        /// Gets a boolean value indicating whether the box is empty.
        /// </summary>
        /// <value>
        /// Indicates whether the box is empty.
        /// </value>
        /// <remarks>
        /// This property is read-only.
        /// </remarks>
        [XmlIgnore]
        public bool IsEmpty
        {
            get { return this.Width <= 0 || this.Height <= 0 || this.Depth <= 0; }
        }

        /// <summary>
        /// Gets or sets the Y-coordinate of the top edge of the box.
        /// </summary>
        /// <value>
        /// Y-coordinate of the top edge of the box.
        /// </value>
        [XmlIgnore]
        public float Top
        {
            get { return this.Max.Y; }
            set { this.Max.Y = value; }
        }

        /// <summary>
        /// Gets or sets the Y-coordinate of the bottom edge of the box.
        /// </summary>
        /// <value>
        /// Y-coordinate of the bottom edge of the box.
        /// </value>
        [XmlIgnore]
        public float Bottom
        {
            get { return this.Min.Y; }
            set { this.Min.Y = value; }
        }

        /// <summary>
        /// Gets or sets the X-coordinate of the left edge of the box.
        /// </summary>
        /// <value>
        /// X-coordinate of the left edge of the box.
        /// </value>
        [XmlIgnore]
        public float Left
        {
            get { return this.Min.X; }
            set { this.Min.X = value; }
        }

        /// <summary>
        /// Gets or sets the X-coordinate of the right edge of the box.
        /// </summary>
        /// <value>
        /// X-coordinate of the right edge of the box.
        /// </value>
        [XmlIgnore]
        public float Right
        {
            get { return this.Max.X; }
            set { this.Max.X = value; }
        }

        /// <summary>
        /// Gets or sets the Z-coordinate of the near edge of the box.
        /// </summary>
        /// <value>
        /// Z-coordinate of the near edge of the box.
        /// </value>
        [XmlIgnore]
        public float Near
        {
            get { return this.Max.Z; }
            set { this.Max.Z = value; }
        }

        /// <summary>
        /// Gets or sets the Z-coordinate of the far edge of the box.
        /// </summary>
        /// <value>
        /// Z-coordinate of the far edge of the box.
        /// </value>
        [XmlIgnore]
        public float Far
        {
            get { return this.Min.Z; }
            set { this.Min.Z = value; }
        }

        /// <summary>
        /// Gets the width of the box.
        /// </summary>
        /// <value>
        /// Width of the box.
        /// </value>
        /// <remarks>
        /// This property is read-only.
        /// </remarks>
        [XmlIgnore]
        public float Width
        {
            get { return this.Max.X - this.Min.X; }
        }

        /// <summary>
        /// Gets the height of the box.
        /// </summary>
        /// <value>
        /// Height of the box.
        /// </value>
        /// <remarks>
        /// This property is read-only.
        /// </remarks>
        [XmlIgnore]
        public float Height
        {
            get { return this.Max.Y - this.Min.Y; }
        }

        /// <summary>
        /// Gets the depth of the box.
        /// </summary>
        /// <value>
        /// Depth of the box.
        /// </value>
        /// <remarks>
        /// This property is read-only.
        /// </remarks>
        [XmlIgnore]
        public float Depth
        {
            get { return this.Max.Z - this.Min.Z; }
        }
        #endregion

        #region Fields
        /// <summary>
        /// Empty box.
        /// </summary>
        private static readonly Box empty = new Box();

        /// <summary>
        /// Minimum corner of the box.
        /// </summary>
        [XmlElement("min")]
        public Vector3 Min;

        /// <summary>
        /// Maximum corner of the box.
        /// </summary>
        [XmlElement("max")]
        public Vector3 Max;
        #endregion
    }
}
